package com.problem.leetcode;

import java.util.Stack;

/**
 * @author yanzhuoleo
 * @version V1.0
 * @Description: Problem20
 * @date 2018-01-08-上午11:51
 */

public class Problem20 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();

        if (s.length() == 1) {
            return false;
        }

        if (s.length() == 0) {
            return true;
        }

        char[] chars = s.toCharArray();
        char   element;


        for (int index = 0; index < chars.length; ++index) {
            element = chars[index];
            if (element == '{' || element == '[' || element == '(') {
                stack.push(element);
            }

            if (element == '}' || element == ']' || element == ')') {

                if (stack.size() <= 0) {
                    return false;
                }

                char stack1Char = stack.pop();

                if ((element == '}' && stack1Char != '{') ||
                    (element == ']' && stack1Char != '[') ||
                    (element == ')' && stack1Char != '(')) {
                    return false;
                }
            }
        }

        if (stack.size() > 0) {
            return false;
        }

        return true;
    }
}
